package com.tencent.mm.opensdk.openapi;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.tencent.mm.opensdk.a.a.b;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.GetMessageFromWX.Req;
import com.tencent.mm.opensdk.modelmsg.SendAuth.Resp;
import com.tencent.mm.opensdk.modelpay.PayResp;

@SuppressLint("LongLogTag")
final class WXApiImplV10 implements IWXAPI {
    private static final String TAG = "MicroMsg.SDK.WXApiImplV10";
    private String appId;
    private boolean checkSignature;
    private Context context;
    private boolean detached;
    private static String wxappPayEntryClassname;

    static {
        WXApiImplV10.wxappPayEntryClassname = null;
    }

    WXApiImplV10(Context arg4, String arg5, boolean arg6) {

        this.checkSignature = false;
        this.detached = false;
        Log.d("MicroMsg.SDK.WXApiImplV10", "<init>, appId = " + arg5 + ", checkSignature = " + arg6);
        this.context = arg4;
        this.appId = arg5;
        this.checkSignature = arg6;
    }

    private boolean checkSumConsistent(byte[] arg5, byte[] arg6) {
        boolean v1 = false;
        if (arg5 == null || arg5.length == 0 || arg6 == null || arg6.length == 0) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "checkSumConsistent fail, invalid arguments");
        } else if (arg5.length != arg6.length) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "checkSumConsistent fail, length is different");
        } else {
            int v0 = 0;
            while (true) {
                if (v0 >= arg5.length) {
                    break;
                } else if (arg5[v0] == arg6[v0]) {
                    ++v0;
                    continue;
                }

                return v1;
            }

            v1 = true;
        }

        return v1;
    }

    private boolean createChatroom(Context arg9, Bundle arg10) {
        Cursor v0 = arg9.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/createChatroom"), null, null, new String[]{this.appId, arg10.getString("_wxapi_basereq_transaction", ""), arg10.getString("_wxapi_create_chatroom_group_id", ""), arg10.getString("_wxapi_create_chatroom_chatroom_name", ""), arg10.getString("_wxapi_create_chatroom_chatroom_nickname", ""), arg10.getString("_wxapi_create_chatroom_ext_msg", ""), arg10.getString("_wxapi_basereq_openid", "")}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }


    public final void detach() {
        Log.d("MicroMsg.SDK.WXApiImplV10", "detach");
        this.detached = true;
        this.context = null;
    }

    public final int getWXAppSupportAPI() {
        int v0 = 0;
        if (this.detached) {
            throw new IllegalStateException("getWXAppSupportAPI fail, WXMsgImpl has been detached");
        }

        if (!this.isWXAppInstalled()) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "open wx app failed, not installed or signature check failed");
            return v0;
        }

        v0 = new MMSharedPreferences(this.context).getInt("_build_info_sdk_int_", 0);
        if (v0 == 0) {
            try {
                v0 = this.context.getPackageManager().getApplicationInfo("com.tencent.mm", PackageManager.GET_META_DATA).metaData.getInt("com.tencent.mm.BuildInfo.OPEN_SDK_VERSION", 0);
            } catch (Exception v1) {
                Log.e("MicroMsg.SDK.WXApiImplV10", "get from metaData failed : " + v1.getMessage());
            }
        }

        return v0;
    }

    public final boolean handleIntent(Intent arg7, IWXAPIEventHandler arg8) {
        boolean v0 = false;
        try {
            if (!WXApiImplComm.isIntentFromWx(arg7, "com.tencent.mm.openapi.token")) {
                Log.i("MicroMsg.SDK.WXApiImplV10", "handleIntent fail, intent not from weixin msg");
                return v0;
            }

            if (this.detached) {
                throw new IllegalStateException("handleIntent fail, WXMsgImpl has been detached");
            }

            String v2 = arg7.getStringExtra("_mmessage_content");
            int v3 = arg7.getIntExtra("_mmessage_sdkVersion", 0);
            String v4 = arg7.getStringExtra("_mmessage_appPackage");
            if (v4 == null || v4.length() == 0) {
                Log.e("MicroMsg.SDK.WXApiImplV10", "invalid argument");
                return v0;
            }

            if (!this.checkSumConsistent(arg7.getByteArrayExtra("_mmessage_checksum"), b.a(v2, v3, v4))) {
                Log.e("MicroMsg.SDK.WXApiImplV10", "checksum fail");
                return v0;
            }

            int v2_1 = arg7.getIntExtra("_wxapi_command_type", 0);
            switch (v2_1) {
                case 1: {
                    arg8.onResp(new Resp(arg7.getExtras()));
                    return true;
                }
                case 2: {
                    arg8.onResp(new com.tencent.mm.opensdk.modelmsg.SendMessageToWX.Resp(arg7.getExtras()));
                    return true;
                }
                case 3: {
                    arg8.onReq(new Req(arg7.getExtras()));
                    return true;
                }
                case 4: {
                    arg8.onReq(new com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX.Req(arg7.getExtras()));
                    return true;
                }
                case 5: {
                    label_84:
                    arg8.onResp(new PayResp(arg7.getExtras()));
                    return true;
                }
                case 6: {
                    label_90:
                    arg8.onReq(new com.tencent.mm.opensdk.modelmsg.LaunchFromWX.Req(arg7.getExtras()));
                    return true;
                }
                case 9: {
                    arg8.onResp(new com.tencent.mm.opensdk.modelbiz.AddCardToWXCardPackage.Resp(arg7.getExtras()));
                    return true;
                }
                case 12: {
                    arg8.onResp(new com.tencent.mm.opensdk.modelbiz.OpenWebview.Resp(arg7.getExtras()));
                    return true;
                }
                case 14: {
                    arg8.onResp(new com.tencent.mm.opensdk.modelbiz.CreateChatroom.Resp(arg7.getExtras()));
                    return true;
                }
                case 15: {
                    arg8.onResp(new com.tencent.mm.opensdk.modelbiz.JoinChatroom.Resp(arg7.getExtras()));
                    return true;
                }
                case 16: {
                    arg8.onResp(new com.tencent.mm.opensdk.modelbiz.ChooseCardFromWXCardPackage.Resp(arg7.getExtras()));
                    return true;
                }
            }

            Log.e("MicroMsg.SDK.WXApiImplV10", "unknown cmd = " + v2_1);
            return v0;
        } catch (Exception v1) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "handleIntent fail, ex = " + v1.getMessage());
        }

        return v0;
    }

    public final boolean isWXAppInstalled() {
        boolean v0 = false;
        if (this.detached) {
            throw new IllegalStateException("isWXAppInstalled fail, WXMsgImpl has been detached");
        }

        try {
            PackageInfo v1_1 = this.context.getPackageManager().getPackageInfo("com.tencent.mm", PackageManager.GET_SIGNATURES);
            if (v1_1 == null) {
                return v0;
            }

            v0 = WXApiImplComm.validateAppSignature(this.context, v1_1.signatures, this.checkSignature);
        } catch (PackageManager.NameNotFoundException v1) {
        }

        return v0;
    }

    public final boolean isWXAppSupportAPI() {
        if (this.detached) {
            throw new IllegalStateException("isWXAppSupportAPI fail, WXMsgImpl has been detached");
        }

        boolean v0 = this.getWXAppSupportAPI() >= 603979778 ? true : false;
        return v0;
    }

    private boolean joinChatroom(Context arg9, Bundle arg10) {
        Cursor v0 = arg9.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/joinChatroom"), null, null, new String[]{this.appId, arg10.getString("_wxapi_basereq_transaction", ""), arg10.getString("_wxapi_join_chatroom_group_id", ""), arg10.getString("_wxapi_join_chatroom_chatroom_nickname", ""), arg10.getString("_wxapi_join_chatroom_ext_msg", ""), arg10.getString("_wxapi_basereq_openid", "")}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    public final boolean openWXApp() {
        boolean v0 = false;
        if (this.detached) {
            throw new IllegalStateException("openWXApp fail, WXMsgImpl has been detached");
        }

        if (!this.isWXAppInstalled()) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "open wx app failed, not installed or signature check failed");
            return v0;
        }

        try {
            this.context.startActivity(this.context.getPackageManager().getLaunchIntentForPackage("com.tencent.mm"));
            v0 = true;
        } catch (Exception v1) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "startActivity fail, exception = " + v1.getMessage());
        }

        return v0;
    }

    public final boolean registerApp(String arg3) {
        return this.registerApp(arg3, 0);
    }

    public final boolean registerApp(String arg5, long arg6) {
        boolean v0;
        if (this.detached) {
            throw new IllegalStateException("registerApp fail, WXMsgImpl has been detached");
        }

        if (!WXApiImplComm.validateAppSignatureForPackage(this.context, "com.tencent.mm", this.checkSignature)) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "register app failed for wechat app signature check failed");
            v0 = false;
        } else {
            Log.d("MicroMsg.SDK.WXApiImplV10", "registerApp, appId = " + arg5);
            if (arg5 != null) {
                this.appId = arg5;
            }

            Log.d("MicroMsg.SDK.WXApiImplV10", "registerApp, appId = " + arg5);
            if (arg5 != null) {
                this.appId = arg5;
            }

            Log.d("MicroMsg.SDK.WXApiImplV10", "register app " + this.context.getPackageName());
            com.tencent.mm.opensdk.a.a.a.a$a v0_1 = new com.tencent.mm.opensdk.a.a.a.a$a();
            v0_1.e = "com.tencent.mm";
            v0_1.f = "com.tencent.mm.plugin.openapi.Intent.ACTION_HANDLE_APP_REGISTER";
            v0_1.c = "weixin://registerapp?appid=" + this.appId;
            v0_1.g = arg6;
            v0 = com.tencent.mm.opensdk.a.a.a.a(this.context, v0_1);
        }

        return v0;
    }

    private boolean sendAddCardToWX(Context arg8, Bundle arg9) {
        Cursor v0 = arg8.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/addCardToWX"), null, null, new String[]{this.appId, arg9.getString("_wxapi_add_card_to_wx_card_list"), arg9.getString("_wxapi_basereq_transaction")}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    private boolean sendChooseCardFromWX(Context arg8, Bundle arg9) {
        Cursor v0 = arg8.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/chooseCardFromWX"), null, null,
                new String[]{arg9.getString("_wxapi_choose_card_from_wx_card_app_id"), arg9.getString("_wxapi_choose_card_from_wx_card_location_id"),
                        arg9.getString("_wxapi_choose_card_from_wx_card_sign_type"), arg9.getString("_wxapi_choose_card_from_wx_card_card_sign"),
                        arg9.getString("_wxapi_choose_card_from_wx_card_time_stamp"), arg9.getString("_wxapi_choose_card_from_wx_card_nonce_str"),
                        arg9.getString("_wxapi_choose_card_from_wx_card_card_id"), arg9.getString("_wxapi_choose_card_from_wx_card_card_type"),
                        arg9.getString("_wxapi_choose_card_from_wx_card_can_multi_select"), arg9.getString("_wxapi_basereq_transaction")}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    private boolean sendJumpToBizProfileReq(Context arg9, Bundle arg10) {
        Cursor v0 = arg9.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/jumpToBizProfile"), null,
                null, new String[]{this.appId, arg10.getString("_wxapi_jump_to_biz_profile_req_to_user_name"), arg10.getString("_wxapi_jump_to_biz_profile_req_ext_msg"),
                        String.valueOf(arg10.getInt("_wxapi_jump_to_biz_profile_req_scene")), String.valueOf(arg10.getInt("_wxapi_jump_to_biz_profile_req_profile_type"))}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    private boolean sendJumpToBizTempSessionReq(Context arg9, Bundle arg10) {
        Cursor v0 = arg9.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/jumpToBizTempSession"),
                null, null, new String[]{this.appId, arg10.getString("_wxapi_jump_to_biz_webview_req_to_user_name"), arg10.getString("_wxapi_jump_to_biz_webview_req_session_from"),
                        String.valueOf(arg10.getInt("_wxapi_jump_to_biz_webview_req_show_type"))}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    private boolean sendJumpToBizWebviewReq(Context arg9, Bundle arg10) {
        Cursor v0 = arg9.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/jumpToBizProfile"),
                null, null, new String[]{this.appId,
                        arg10.getString("_wxapi_jump_to_biz_webview_req_to_user_name"),
                        arg10.getString("_wxapi_jump_to_biz_webview_req_ext_msg"),
                        String.valueOf(arg10.getInt("_wxapi_jump_to_biz_webview_req_scene"))}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    private boolean sendOpenBusiLuckyMoney(Context arg8, Bundle arg9) {
        Cursor v0 = arg8.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/openBusiLuckyMoney"), null,
                null, new String[]{this.appId, arg9.getString("_wxapi_open_busi_lucky_money_timeStamp"),
                        arg9.getString("_wxapi_open_busi_lucky_money_nonceStr"),
                        arg9.getString("_wxapi_open_busi_lucky_money_signType"),
                        arg9.getString("_wxapi_open_busi_lucky_money_signature"),
                        arg9.getString("_wxapi_open_busi_lucky_money_package")}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    private boolean sendOpenRankListReq(Context arg7, Bundle arg8) {
        Cursor v0 = arg7.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/openRankList"), null, null, new String[0], null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    private boolean sendOpenWebview(Context arg8, Bundle arg9) {
        Cursor v0 = arg8.getContentResolver().query(Uri.parse("content://com.tencent.mm.sdk.comm.provider/openWebview"), null, null,
                new String[]{this.appId, arg9.getString("_wxapi_jump_to_webview_url"), arg9.getString("_wxapi_basereq_transaction")}, null);
        if (v0 != null) {
            v0.close();
        }

        return true;
    }

    private boolean sendPayReq(Context arg5, Bundle arg6) {
        boolean v0_1;
        String v2 = null;
        if (WXApiImplV10.wxappPayEntryClassname == null) {
            WXApiImplV10.wxappPayEntryClassname = new MMSharedPreferences(arg5).getString("_wxapp_pay_entry_classname_", v2);
            Log.d("MicroMsg.SDK.WXApiImplV10", "pay, set wxappPayEntryClassname = " + WXApiImplV10.wxappPayEntryClassname);
            if (WXApiImplV10.wxappPayEntryClassname == null) {
                try {
                    WXApiImplV10.wxappPayEntryClassname = arg5.getPackageManager().getApplicationInfo("com.tencent.mm", PackageManager.GET_META_DATA).metaData.getString("com.tencent.mm.BuildInfo.OPEN_SDK_PAY_ENTRY_CLASSNAME", null);
                } catch (Exception v0) {
                    Log.e("MicroMsg.SDK.WXApiImplV10", "get from metaData failed : " + v0.getMessage());
                }
            }

            if (WXApiImplV10.wxappPayEntryClassname != null) {
                com.tencent.mm.opensdk.a.aa.a$a v0_2 = new com.tencent.mm.opensdk.a.aa.a$a();
                v0_2.d = arg6;
                v0_2.a = "com.tencent.mm";
                v0_2.b = WXApiImplV10.wxappPayEntryClassname;
                v0_1 = com.tencent.mm.opensdk.a.aa.a(arg5, v0_2);
            } else {
                Log.e("MicroMsg.SDK.WXApiImplV10", "pay fail, wxappPayEntryClassname is null");
                v0_1 = false;
            }
        } else {
            com.tencent.mm.opensdk.a.aa.a$a v0_2 = new com.tencent.mm.opensdk.a.aa.a$a();
            v0_2.d = arg6;
            v0_2.a = "com.tencent.mm";
            v0_2.b = WXApiImplV10.wxappPayEntryClassname;
            v0_1 = com.tencent.mm.opensdk.a.aa.a(arg5, v0_2);
        }

        return v0_1;
    }

    public final boolean sendReq(BaseReq arg5) {
        boolean v0 = false;
        if (this.detached) {
            throw new IllegalStateException("sendReq fail, WXMsgImpl has been detached");
        }

        if (!WXApiImplComm.validateAppSignatureForPackage(this.context, "com.tencent.mm", this.checkSignature)) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "sendReq failed for wechat app signature check failed");
        } else if (!arg5.checkArgs()) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "sendReq checkArgs fail");
        } else {
            Log.d("MicroMsg.SDK.WXApiImplV10", "sendReq, req type = " + arg5.getType());
            Bundle v0_1 = new Bundle();
            arg5.toBundle(v0_1);
            if (arg5.getType() == 5) {
                v0 = this.sendPayReq(this.context, v0_1);
            } else if (arg5.getType() == 7) {
                v0 = this.sendJumpToBizProfileReq(this.context, v0_1);
            } else if (arg5.getType() == 8) {
                v0 = this.sendJumpToBizWebviewReq(this.context, v0_1);
            } else if (arg5.getType() == 10) {
                v0 = this.sendJumpToBizTempSessionReq(this.context, v0_1);
            } else if (arg5.getType() == 9) {
                v0 = this.sendAddCardToWX(this.context, v0_1);
            } else if (arg5.getType() == 16) {
                v0 = this.sendChooseCardFromWX(this.context, v0_1);
            } else if (arg5.getType() == 11) {
                v0 = this.sendOpenRankListReq(this.context, v0_1);
            } else if (arg5.getType() == 12) {
                v0 = this.sendOpenWebview(this.context, v0_1);
            } else if (arg5.getType() == 13) {
                v0 = this.sendOpenBusiLuckyMoney(this.context, v0_1);
            } else if (arg5.getType() == 14) {
                v0 = this.createChatroom(this.context, v0_1);
            } else if (arg5.getType() == 15) {
                v0 = this.joinChatroom(this.context, v0_1);
            } else {
                com.tencent.mm.opensdk.a.aa.a$a v1 = new com.tencent.mm.opensdk.a.aa.a$a();
                v1.d = v0_1;
                v1.c = "weixin://sendreq?appid=" + this.appId;
                v1.a = "com.tencent.mm";
                v1.b = "com.tencent.mm.plugin.base.stub.WXEntryActivity";
                v0 = com.tencent.mm.opensdk.a.aa.a(this.context, v1);
            }
        }

        return v0;
    }

    public final boolean sendResp(BaseResp arg5) {
        boolean v0 = false;
        if (this.detached) {
            throw new IllegalStateException("sendResp fail, WXMsgImpl has been detached");
        }

        if (!WXApiImplComm.validateAppSignatureForPackage(this.context, "com.tencent.mm", this.checkSignature)) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "sendResp failed for wechat app signature check failed");
        } else if (!arg5.checkArgs()) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "sendResp checkArgs fail");
        } else {
            Bundle v0_1 = new Bundle();
            arg5.toBundle(v0_1);
            com.tencent.mm.opensdk.a.aa.a$a v1 = new com.tencent.mm.opensdk.a.aa.a$a();
            v1.d = v0_1;
            v1.c = "weixin://sendresp?appid=" + this.appId;
            v1.a = "com.tencent.mm";
            v1.b = "com.tencent.mm.plugin.base.stub.WXEntryActivity";
            v0 = com.tencent.mm.opensdk.a.aa.a(this.context, v1);
        }

        return v0;
    }

    public final void unregisterApp() {
        if (this.detached) {
            throw new IllegalStateException("unregisterApp fail, WXMsgImpl has been detached");
        }

        if (!WXApiImplComm.validateAppSignatureForPackage(this.context, "com.tencent.mm", this.checkSignature)) {
            Log.e("MicroMsg.SDK.WXApiImplV10", "unregister app failed for wechat app signature check failed");
        } else {
            Log.d("MicroMsg.SDK.WXApiImplV10", "unregisterApp, appId = " + this.appId);
            if (this.appId != null && this.appId.length() != 0) {
                Log.d("MicroMsg.SDK.WXApiImplV10", "unregister app " + this.context.getPackageName());
                com.tencent.mm.opensdk.a.a.a.a$a v0 = new com.tencent.mm.opensdk.a.a.a.a$a();
                v0.e = "com.tencent.mm";
                v0.f = "com.tencent.mm.plugin.openapi.Intent.ACTION_HANDLE_APP_UNREGISTER";
                v0.c = "weixin://unregisterapp?appid=" + this.appId;
                com.tencent.mm.opensdk.a.a.a.a(this.context, v0);
                return;
            }

            Log.e("MicroMsg.SDK.WXApiImplV10", "unregisterApp fail, appId is empty");
        }
    }
}

